#include <func.h>
typedef struct{
    pthread_mutex_t mutex;
    pthread_cond_t cond;
}thdata_t;
void cleanup(void *p)
{
    thdata_t *pt=(thdata_t*)p;
    pthread_mutex_unlock(&pt->mutex);
}
void *thfunc(void *p)
{
    printf("I am subthread!\n");
    thdata_t* pt=(thdata_t*)p;
    pthread_mutex_lock(&pt->mutex);
    pthread_cleanup_push(cleanup,pt);
    pthread_cond_wait(&pt->cond,&pt->mutex);
    pthread_cleanup_pop(1);
    printf("Wake succeed!\n");
    pthread_exit(NULL);
}
int main(int argc,char *argv[])
{
    thdata_t t;
    int ret;
    int i,j;
    pthread_t thid[2];
    int thret;
    printf("I am main thread!\n");
    pthread_mutex_init(&t.mutex,NULL);
    pthread_cond_init(&t.cond,NULL);
    for(i=0;i<2;++i)
    {
        pthread_create(thid+i,NULL,thfunc,&t);
    }
    sleep(1);
    pthread_cond_broadcast(&t.cond);
    for(i=0;i<2;++i)
    {
        pthread_cancel(thid[i]);
    }
    for(j=0;j<2;++j)//若该循环中也使用变量i会出现问题
    {
        pthread_join(thid[j],(void**)&thret);
        printf("Thread%d Exit succeed!retnum=%d\n",j+1,thret);
    }
    pthread_mutex_destroy(&t.mutex);
    pthread_cond_destroy(&t.cond);
    return 0;
}

